﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;

    public class LocAddr : BasePage
    {
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chbIsActive;
        protected CheckBox chbIsWeekEndWork;
        protected HtmlForm F2261;
        protected HtmlInputHidden hdnFrom;
        protected Label lblAddress;
        protected Label lblCity;
        protected Label lblContactName;
        protected Label lblEmail;
        protected Label lblFax;
        protected Label lblLocAddrID;
        protected Label lblLocSiteID;
        protected Label lblMsg;
        protected Label lblOwner;
        protected Label lblProvince;
        protected Label lblRemark;
        protected Label lblTelephone;
        protected Label lblZip;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtAddress;
        protected TextBox txtCity;
        protected TextBox txtContactName;
        protected TextBox txtEmail;
        protected TextBox txtFax;
        protected TextBox txtLocAddrID;
        protected TextBox txtLocSiteID;
        protected TextBox txtProvince;
        protected TextBox txtRemark;
        protected TextBox txtTelphone;
        protected TextBox txtZip;
        protected UpdatePanel up1;

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtLocAddrID.Text = "";
            this.txtLocAddrID.Enabled = true;
            this.chbIsActive.Checked = true;
            base.DataEntityViewState = null;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.txtLocAddrID.Text.Trim() != "")
                {
                    List<string> locAddrIDs = new List<string> {
                        this.txtLocAddrID.Text.Trim()
                    };
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(locAddrIDs, "LOC_ADDR") + base.GetGlobalMsgResource("C006");
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnCopy.Enabled = false;
                    base.DataEntityViewState = null;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("LocAddrQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("LocAddrList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.txtLocAddrID.Text.Trim() == "")
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2550_001");
            }
            else if (string.IsNullOrEmpty(this.txtLocSiteID.Text))
            {
                this.lblMsg.Text = "请输入节点代码!";
            }
            else if (base.DataEntityViewState != null)
            {
                this.UpdateLocAddrInfo();
            }
            else
            {
                this.InsertLocAddrInfo();
            }
        }

        private LocAddrInfo GetLocAddrInfo(LocAddrInfo locAddrInfo)
        {
            locAddrInfo.LocAddrID = this.txtLocAddrID.Text.Trim();
            locAddrInfo.LocSiteID = this.txtLocSiteID.Text.Trim();
            locAddrInfo.ContactName = this.txtContactName.Text.Trim();
            locAddrInfo.Telephone = this.txtTelphone.Text.Trim();
            locAddrInfo.Email = this.txtEmail.Text.Trim();
            locAddrInfo.Fax = this.txtFax.Text.Trim();
            locAddrInfo.Province = this.txtProvince.Text.Trim();
            locAddrInfo.City = this.txtCity.Text.Trim();
            locAddrInfo.Zip = this.txtZip.Text.Trim();
            locAddrInfo.Address = this.txtAddress.Text.Trim();
            locAddrInfo.Remark = this.txtRemark.Text.Trim();
            locAddrInfo.IsActive = this.chbIsActive.Checked ? "Y" : "N";
            locAddrInfo.IsWeekendWork = this.chbIsWeekEndWork.Checked ? "Y" : "N";
            return locAddrInfo;
        }

        private void InsertLocAddrInfo()
        {
            try
            {
                LocAddrInfo locAddrInfo = new LocAddrInfo();
                locAddrInfo = this.GetLocAddrInfo(locAddrInfo);
                locAddrInfo.CreatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Insert(locAddrInfo, "LOC_ADDR") + base.GetGlobalMsgResource("C004");
                base.DataEntityViewState = locAddrInfo;
                this.txtLocAddrID.Enabled = false;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C005") + ex.Message;
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.SetLocAddrInfo();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetLocAddrInfo()
        {
            try
            {
                string locAddrID = base.Request.QueryString["ID"].ToString();
                LocAddrInfo locAddrInfo = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(locAddrID, "LOC_ADDR") as LocAddrInfo;
                base.DataEntityViewState = locAddrInfo;
                if (locAddrInfo != null)
                {
                    this.txtLocAddrID.Text = locAddrInfo.LocAddrID;
                    this.txtLocAddrID.Enabled = false;
                    this.txtLocSiteID.Text = locAddrInfo.LocSiteID;
                    this.txtContactName.Text = locAddrInfo.ContactName;
                    this.txtTelphone.Text = locAddrInfo.Telephone;
                    this.txtEmail.Text = locAddrInfo.Email;
                    this.txtFax.Text = locAddrInfo.Fax;
                    this.txtProvince.Text = locAddrInfo.Province;
                    this.txtCity.Text = locAddrInfo.City;
                    this.txtZip.Text = locAddrInfo.Zip;
                    this.txtAddress.Text = locAddrInfo.Address;
                    this.txtRemark.Text = locAddrInfo.Remark;
                    this.chbIsActive.Checked = locAddrInfo.IsActive == "Y";
                    this.chbIsWeekEndWork.Checked = locAddrInfo.IsWeekendWork == "Y";
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateLocAddrInfo()
        {
            try
            {
                LocAddrInfo locAddrInfo = base.DataEntityViewState as LocAddrInfo;
                locAddrInfo = this.GetLocAddrInfo(locAddrInfo);
                locAddrInfo.UpdatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Update(locAddrInfo, "LOC_ADDR") + base.GetGlobalMsgResource("C002");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C003") + ex.Message;
            }
        }
    }
}

